﻿using ShiQuan.Logger;
using ShiQuan.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Console.WriteLine("输入Redis IP：");
                //string ip = Console.ReadLine();
                //Console.WriteLine("port：");
                //int port = Convert.ToInt32(Console.ReadLine());
                using (RedisClient client = new RedisClient("127.0.0.1", 6381))//127.0.0.1:6381
                {
                    Console.WriteLine("开始连接：" + client.IP + " " + client.Port);
                    bool connect = client.Connect();
                    if (connect == false)
                    {
                        Console.WriteLine("连接失败！");
                        return;
                    }
                    client.Set<TestItem>("classId", new TestItem() { ClassId="classId",ClassName="className"});

                    var testItem = client.Get<TestItem>("classId");
                    Console.WriteLine("ClassId=" + testItem.ClassId + " ClassName=" + testItem.ClassName);
                    string result = "";
                    var content = "";
                    do
                    {
                        Console.Write(client.IP + ":" + client.Port + ">");
                        content = Console.ReadLine().ToLower().Trim();
                        if (string.IsNullOrEmpty(content) || content == "")
                            continue;
                        
                        string[] commands = content.Split(' ');
                        switch (commands[0])
                        {
                            case "set":
                            {
                                string key = commands[1];
                                string value = commands[2];
                                ThreadLogHelper.Info("set","key=" + key + " value=" + value);
                                result = client.Set(key, value);
                                Console.WriteLine(result);
                                break;
                            }
                            case "get":
                            {
                                string key = commands[1];
                                ThreadLogHelper.Info("get", "key=" + key);
                                result = client.Get(key);
                                Console.WriteLine(result);
                                break;
                            }
                            case "quit":
                            {
                                ThreadLogHelper.Info("quit", content);
                                result = client.QUIT();
                                Console.WriteLine(result);
                                break;
                            }
                            default:
                            {
                                ThreadLogHelper.Info(commands[0], content);
                                result = client.Send(commands);
                                Console.WriteLine(result);
                                break;
                            }
                        }

                        if (content == "quit")
                            break;

                    } while (true);
                }
            }
            catch (Exception ex)
            {
                ThreadLogHelper.Error("异常", ex);
                Console.WriteLine("异常：" + ex.Message);
            }
            finally
            {
                Console.WriteLine("按任意键退出...");
                Console.ReadLine();
            }
        }
    }
    public class TestItem
    {
        public string ClassId { get; set; }
        public string ClassName { get; set; }
    }
}
